//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <aura.h>

static void threadEntry(Aura_PVoid pArgs)
{
    Aura_Handle hEvent = (Aura_Handle)pArgs;
    Aura_ECode ec = AURA_NOERROR;

    Aura_WaitResult wr;
    Aura_EventState es;
    ec = AuraEvent_Wait(hEvent, AURA_INFINITE, &wr, &es);
    if (AURA_FAILED(ec) || (Aura_WaitResult_OK != wr)) {
        Aura_printf(" child: <AuraEvent_Wait> failed. (ec = 0x%x)\n",ec);
        return;
    }
    Aura_ThreadId threadId = AURA_INVALID_TID;
    AuraThread_GetCurrentId(&threadId);
    Aura_printf(" child: thread id = %d\n", threadId);
}

Aura_Bool AuraTest_Event()
{
    Aura_Handle hEvent = AURA_NULL;
    Aura_ECode ec = AURA_NOERROR;
    Aura_ThreadId threadId = AURA_INVALID_TID;
    Aura_Handle hThread = AURA_NULL;

    ec = AuraEvent_Create(AURA_FALSE, AURA_UNSIGNALED, &hEvent);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraEvent_Create. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }


    ec = AuraThread_Create(threadEntry, (Aura_PVoid)hEvent, 0, &hThread, &threadId);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Create. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }
    Aura_printf(" father: new thread id = %d\n", threadId);

    ec = AuraEvent_Signal(hEvent, AURA_SIGNALED);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraEvent_Signal. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    Aura_WaitResult wr;
    ec = AuraThread_Join(hThread, AURA_INFINITE, &wr);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_Join. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }
    Aura_QuitCode quitCode;
    ec = AuraThread_GetQuitCode(hThread, &quitCode);
    if (AURA_FAILED(ec)) {
        Aura_printf("File: %s Line: %d, AuraThread_GetQuitCode. (ec = 0x%x)\n",
            __FILE__,__LINE__, ec);
        goto ErrorExit;
    }

    AuraThread_Destroy(hThread);
    hThread = AURA_NULL;
    AuraEvent_Destroy(hEvent);
    hEvent = AURA_NULL;
    return AURA_TRUE;

ErrorExit:
    if (AURA_NULL != hThread) {
        AuraThread_Destroy(hThread);
        hThread = AURA_NULL;
    }
    if (AURA_NULL != hEvent) {
        AuraEvent_Destroy(hEvent);
        hEvent = AURA_NULL;
    }
    return AURA_FALSE;
}

